import React, {Component} from 'react';
import request from '@/utils/request';
import {Button, notification, Icon} from 'antd';
import moment from 'moment'
import BasicMap from "./BasicMap";
import DeviceList from "./DeviceList";
import StayList from "./StayList";
class Index extends Component {
    constructor(props) {
        super(props);
        this.state = {
            deviceListVisible: true,
            isTrail: false,
            stayListVisible:true,
            lineData:[]
        };
    }
    componentDidMount() {
        this.getCurrentLine();
    }
    getCurrentLine = async (device) => {
        if (localStorage.getItem('user')) {
            var user = JSON.parse(localStorage.getItem('user')).username;
            var company_id = JSON.parse(localStorage.getItem('user')).company_id;
        }
        var url=`device_line/?device_id=1893058637&day=2020-01-19&user=${user}&company_id=${company_id}`;
        const response = await new Promise(resolve => {
            request({url}).then(res => {
                var fd = [];
                if (res.data.data) {
                    res.data.data.forEach(function (item) {
                        if (item.outlier === 0) {
                            item.device_id=1893058637;
                            fd.push(item);
                        }
                    });
                    res.data.data = fd;
                }
                resolve(res.data)
            });
        });
        console.log(response)
        if (response.data.length > 0) {
            var center = {
                lng: response.data[0].lat_lng[1],
                lat: response.data[0].lat_lng[0]
            }
        } else {
            center = {lng: 108.93, lat: 34.27, name: 'xa'}
        }
        if (response.data.length === 0) {
            var that = this;
            this.getStayPoints(this.state.checkedKeys).then(res => {
                if (res[0].length > 0) {
                    // center=res[0][0];
                    // this.setState({
                    //     center
                    // })
                } else {
                    that.openNotification(today, device_id);
                }
            })
        }
        this.setState({
            lineData: response,
            center
        })

    };
    /**
     *  地图表格的关闭与显示
     */
    handleStayListVisible = () => {
        this.setState({
            stayListVisible: !this.state.stayListVisible,
        })
    };
    render() {
        const {
             isTrail,
             deviceListVisible,
             stayListVisible,
             lineData
        } = this.state;

        const basicMapProps = {
            lineData
        };

        return (
            <div>
                <BasicMap {...basicMapProps}>
                </BasicMap>
                <DeviceList
                            visible={deviceListVisible}
                            isTrail={isTrail}
                />
                <StayList
                          visible={stayListVisible}
                          onClose={this.handleStayListVisible}/>
            </div>
        );
    }
}


export default Index;